package com.bigstar.lolkor;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigstar.lolkor.Lolkor.SaleInfo;

public class HistoryDialog extends Dialog implements OnClickListener {
	String nick_name;
	String history;
	String name;
	Context context;
	boolean noti;
	String tip;
	String tip2;
	boolean sale;
    public ArrayList<SaleInfo> sale_skin;
    public ArrayList<SaleInfo> sale_champ;
    private Dialog my_dialog = null;
    String sale_text;
	public HistoryDialog(Context context, String nick_name, ArrayList<SaleInfo> skinInfo, ArrayList<SaleInfo> champInfo, String sale_text) {
		super(context, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
		this.context = context;
		this.nick_name = nick_name;
		this.sale = true;
		this.noti = false;
		sale_skin = skinInfo;
		sale_champ = champInfo;
		this.sale_text = sale_text;
	}
	
	public HistoryDialog(Context context, String nick_name, String history) {
		super(context, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
		this.context = context;
		this.nick_name = nick_name;
		this.history = history;
		this.noti = true;
	}
	
	public HistoryDialog(Context context, String name, String nickname, String history, String tip, String tip2) {
		super(context, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
		this.context = context;
		this.name = name;
		this.nick_name = name+", "+nickname;
		this.history = history;
		this.tip = tip;
		this.tip2 = tip2;
		this.noti = false;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.history);
		TextView tv;
		Button b;
		tv = (TextView) findViewById(R.id.his_name);
		tv.setText(nick_name);
		b = (Button) findViewById(R.id.his_close);
		b.setOnClickListener(this);
		if(sale){
			tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
			LinearLayout layout = (LinearLayout)findViewById(R.id.btn_histip);
			layout.setVisibility(View.GONE);
			layout = (LinearLayout)findViewById(R.id.lay_tip);
			layout.setVisibility(View.GONE);
			layout = (LinearLayout)findViewById(R.id.sale_info_layout);
			layout.setVisibility(View.VISIBLE);
			layout = (LinearLayout)findViewById(R.id.sale_skin_layout);
	    	for(int i = 0; i < sale_skin.size(); i++){
				 View convertView = LayoutInflater.from(context).inflate(R.layout.sale_champ_list_row, null); 
				 Bitmap bm = Utils.getBitmapFromUrl(sale_skin.get(i).img);
				 ImageView champ_img = (ImageView)convertView.findViewById(R.id.champ_img); 
				 champ_img.setImageBitmap(bm);
				 TextView ip_point = (TextView)convertView.findViewById(R.id.tv_rp_point); 
				 ip_point.setText(""+sale_skin.get(i).price);
				 champ_img.setOnClickListener(this);
				 champ_img.setTag(i);
				 convertView.setPadding(0, 0, 7, 0);
				 layout.addView(convertView);
	    	}    	
			layout = (LinearLayout)findViewById(R.id.sale_champ_layout);
	    	for(int i = 0; i < sale_champ.size(); i++){
	    		Log.i("sale_champ", sale_champ.get(i).url);
				 View convertView = LayoutInflater.from(context).inflate(R.layout.sale_champ_list_row, null); 
				 Bitmap bm = Utils.getBitmapFromUrl(sale_champ.get(i).img);
				 ImageView champ_img = (ImageView)convertView.findViewById(R.id.champ_img); 
				 champ_img.setImageBitmap(bm);
				 TextView ip_point = (TextView)convertView.findViewById(R.id.tv_rp_point); 
				 ip_point.setText(""+sale_champ.get(i).price);
				 convertView.setOnClickListener(this);
				 convertView.setTag(i);
				 convertView.setPadding(0, 0, 7, 0);
				 layout.addView(convertView);
	    	}
	    	tv = (TextView)findViewById(R.id.sale_info_text);
	    	tv.setText(Html.fromHtml(sale_text));
			return;
		}else if(noti){
			LinearLayout layout = (LinearLayout)findViewById(R.id.btn_histip);
			layout.setVisibility(View.GONE);
			layout = (LinearLayout)findViewById(R.id.lay_tip);
			layout.setVisibility(View.GONE);
		}else{
			LinearLayout layout = (LinearLayout)findViewById(R.id.lay_tip);
			layout.setVisibility(View.GONE);
			tv = (TextView)findViewById(R.id.his_text);
			tv.setVisibility(View.VISIBLE);
			b = (Button) findViewById(R.id.btn_his);
			b.setOnClickListener(this);
			b = (Button) findViewById(R.id.btn_tip);
			b.setOnClickListener(this);
			tv = (TextView) findViewById(R.id.tip_0_name);
			tv.setText("플레이할 때 : "+name);
			tv = (TextView) findViewById(R.id.tip_0_txt);
			tv.setText(Html.fromHtml(tip));
			tv = (TextView) findViewById(R.id.tip_1_name);
			tv.setText("상대할 때 : "+name);
			tv = (TextView) findViewById(R.id.tip_1_txt);
			tv.setText(Html.fromHtml(tip2));
		}
		tv = (TextView) findViewById(R.id.his_text);
		tv.setText(history);
	}
	int position;
	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.his_close){
			this.dismiss();	
		}else if(v.getId() == R.id.btn_his){
			LinearLayout layout = (LinearLayout)findViewById(R.id.lay_tip);
			layout.setVisibility(View.GONE);
			TextView tv = (TextView)findViewById(R.id.his_text);
			tv.setVisibility(View.VISIBLE);
		}else if(v.getId() == R.id.btn_tip){
			TextView tv = (TextView)findViewById(R.id.his_text);
			tv.setVisibility(View.GONE);
			LinearLayout layout = (LinearLayout)findViewById(R.id.lay_tip);
			layout.setVisibility(View.VISIBLE);
		//	tv = (TextView)findViewById(R.id.tip_1_txt);
		//	tv.setVisibility(View.VISIBLE);
		}else if(v.getId() == R.id.champ_img){
			position = (Integer) v.getTag();
    		Handler h = new Handler(context.getMainLooper());
    		h.postDelayed(new Runnable()
    		{
    		    @Override
    		    public void run()
    		    {
    				if (my_dialog == null) {
    					my_dialog = new MyWaitDialog(context);
    					my_dialog.show();
    				}
    				Utils.skin = null;
    				Utils.skin = Utils.getBitmapFromUrl(sale_skin.get(position).url);	
    				if(Utils.skin == null){
    					return;
    				}
    				ImageView iv = new ImageView(context);
    				iv.setImageBitmap(Utils.skin);
    				new AlertDialog.Builder(context)
    				.setTitle("세일 스킨")
    				.setView(iv)
    				.setPositiveButton("크게보기", new DialogInterface.OnClickListener() {
    					public void onClick(DialogInterface dialog, int whichButton) {
			        		Intent intent = new Intent(context, Skin.class);
			        		context.startActivity(intent);
    					}
    				})
    				.setNegativeButton("닫기", new DialogInterface.OnClickListener() {
    					public void onClick(DialogInterface dialog, int whichButton) {
    					}
    				})
    				.create().show();
    				if (my_dialog != null) {
    					my_dialog.dismiss();
    					my_dialog = null;
    				}
    		    }
    		}, 0);	
		}else {
			try{
				int position = (Integer) v.getTag();
				//	Log.i("www", ""+sale_champ.get(position).url.substring(58, sale_champ.get(position).url.length()));
				
					int champ_num = Integer.parseInt(sale_champ.get(position).url.substring(58, sale_champ.get(position).url.length()));
		    		Intent intent = new Intent(context, Champion.class);
		    		for(int j = 0; j < ChampionData.champions.size(); j++){
		    			if(ChampionData.champions.get(j).menual == champ_num){
		    				champ_num = j;
		    				break;
		    			}
		    		}
	    			MenualSelectDialog dialog = new MenualSelectDialog(context, champ_num);
	    			dialog.show();
			}catch(Exception e){
				
			}
		}
	}
}